package DataStorage;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import HibernateSQLite.java.model.MesureText;

public class MesureTextDAOHibernate extends
		GenericDAOHibernate<MesureText, Long, IMesureTextDAO> implements
		IMesureTextDAO {

	@SuppressWarnings("unchecked")
	public boolean isExist(String N_serie, String chemin, String N_cordon,
			String Vue, String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("chemin", chemin))
					.add(Restrictions.eq("N_cordon", N_cordon))
					.add(Restrictions.eq("Vue", Vue))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	public boolean isExist_L1_mesure(MesureText mesureText) {
		if (mesureText.getL1_mesure().equals("")) {
			return false;
		}
		return true;
	}

	public boolean isExist_L2_mesure(MesureText mesureText) {
		if (mesureText.getL2_mesure().equals("")) {
			return false;
		}
		return true;
	}

	public boolean isExist_P1_mesure(MesureText mesureText) {
		if (mesureText.getP1_mesure().equals("")) {
			return false;
		}
		return true;
	}

	public boolean isExist_P2_mesure(MesureText mesureText) {
		if (mesureText.getP2_mesure().equals("")) {
			return false;
		}
		return true;
	}

	public boolean isExist_A_mesure(MesureText mesureText) {
		if (mesureText.getA_mesure().equals("")) {
			return false;
		}
		return true;
	}

	public boolean isExist_B_mesure(MesureText mesureText) {
		if (mesureText.getB_mesure().equals(null)) {
			return false;
		}
		return true;
	}

	// pour tester une mesure
	@SuppressWarnings("unchecked")
	public boolean isExistFlag_mesure(String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return false;
	}

	// pour tester une essieu
	@SuppressWarnings("unchecked")
	public boolean isExist(String N_serie, String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			if (!list.isEmpty()) {
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	public MesureText getMesureText(long mesureText_id) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("mesureText_id", mesureText_id))
					.list();
			for (MesureText mesureText : list) {
				return mesureText;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public MesureText getMesureText(String N_serie, String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			for (MesureText mesureText : list) {
				return mesureText;
			}

		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public MesureText getMesureText(String N_serie, String chemin,
			String N_cordon, String Vue, String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("chemin", chemin))
					.add(Restrictions.eq("N_cordon", N_cordon))
					.add(Restrictions.eq("Vue", Vue))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			if (!list.isEmpty()) {
				for (MesureText mesureText : list) {
					return mesureText;
				}
			} else {
				System.out
						.println("Selection Measure failed, not have this Measure! ");
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getAllListMesureText() {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByFlag_mesure(String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByN_serie(String N_serie,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByChemin(String chemin,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("chemin", chemin))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByN_cordon(String N_cordon,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_cordon", N_cordon))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByVue(String Vue,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("Vue", Vue))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByL1_mesure(String L1_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("L1_mesure", L1_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByL2_mesure(String L2_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("L2_mesure", L2_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByP1_mesure(String P1_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("P1_mesure", P1_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByP2_mesure(String P2_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("P2_mesure", P2_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByA_mesure(String a_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("a_mesure", a_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public List<MesureText> getListMesureTextByB_mesure(String b_mesure,
			String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("b_mesure", b_mesure))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public String getImagePath(String N_serie, String chemin, String N_cordon,
			String Vue, String flag_mesure) {
		try {
			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("chemin", chemin))
					.add(Restrictions.eq("N_cordon", N_cordon))
					.add(Restrictions.eq("Vue", Vue))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();

			String imgePath = "";
			for (MesureText mesureText : list) {
				imgePath = mesureText.getImage_path();
			}

			return imgePath;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;

	}

	@SuppressWarnings("unchecked")
	public String getImagePath(MesureText mesureText) {
		try {

			List<MesureText> list = getSession()
					.createCriteria(MesureText.class)
					.add(Restrictions.eq("N_serie", mesureText.getN_serie()))
					.add(Restrictions.eq("chemin", mesureText.getChemin()))
					.add(Restrictions.eq("N_cordon", mesureText.getN_cordon()))
					.add(Restrictions.eq("Vue", mesureText.getVue()))
					.add(Restrictions.eq("flag_mesure",
							mesureText.getFlag_mesure())).list();

			String imgePath = "";
			for (MesureText m : list) {
				imgePath = m.getImage_path();
			}

			return imgePath;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	// Function
	public int getNumberMesureText(String flag_mesure) {
		List<MesureText> list = getListMesureTextByFlag_mesure(flag_mesure);
		int numberEssieu = list.size();
		return numberEssieu;
	}

	public int getNumberN_cordonByEssieu(String N_serie, String flag_mesure) {
		try {
			Criteria criteria = getSession().createCriteria(MesureText.class);
			ProjectionList proList = Projections.projectionList();
			criteria.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			proList.add(Projections.distinct(Projections.property("N_cordon")));
			criteria.setProjection(proList);
			int numberEssieu = criteria.list().size();

			return numberEssieu;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return (Integer) null;
	}

	public int getNumberVueByN_cordon(String N_serie, String N_cordon,
			String flag_mesure) {
		try {
			Criteria criteria = getSession().createCriteria(MesureText.class);
			ProjectionList proList = Projections.projectionList();
			criteria.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("N_cordon", N_cordon))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			proList.add(Projections.distinct(Projections.property("Vue")));
			criteria.setProjection(proList);
			int numberVue = criteria.list().size();

			return numberVue;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return (Integer) null;
	}

	@SuppressWarnings("unchecked")
	public List<String> getDistinctListN_cordon(String N_serie,
			String flag_mesure) {
		try {
			Criteria criteria = getSession().createCriteria(MesureText.class);
			ProjectionList proList = Projections.projectionList();
			criteria.add(Restrictions.eq("N_serie", N_serie))
					.add(Restrictions.eq("flag_mesure", flag_mesure)).list();
			proList.add(Projections.distinct(Projections.property("N_cordon")));
			criteria.setProjection(proList);

			List<String> list = criteria.list();

			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
		return null;
	}

	public void saveMesureText(String N_serie, String chemin, String N_cordon,
			String Vue, String L1_mesure, String L2_mesure, String P1_mesure,
			String P2_mesure, String a_mesure, String b_mesure,
			String image_path, int flag_mesure) {

		MesureText mesureText = null;
		try {
			String flag_mesure_String = Integer.toString(flag_mesure);
			if (!isExist(N_serie, chemin, N_cordon, Vue, flag_mesure_String)) {
				mesureText = new MesureText();
				mesureText.setN_serie(N_serie);
				mesureText.setChemin(chemin);
				mesureText.setN_cordon(N_cordon);
				mesureText.setVue(Vue);
				mesureText.setL1_mesure(L1_mesure);
				mesureText.setL2_mesure(L2_mesure);
				mesureText.setP1_mesure(P1_mesure);
				mesureText.setP2_mesure(P2_mesure);
				mesureText.setA_mesure(a_mesure);
				mesureText.setB_mesure(b_mesure);
				mesureText.setImage_path(image_path);
				mesureText.setFlag_mesure(Integer.toString(flag_mesure));

				getSession().save(mesureText);
			}
			this.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}
	}

	// pour delete tous les mesure texts par une essieu
	public void deleteMesureTexts(String N_serie, String flag_mesure) {
		try {
			if (this.isExist(N_serie, flag_mesure)) {
				List<MesureText> listMesureText = getListMesureTextByN_serie(
						N_serie, flag_mesure);
				this.deleAll(listMesureText);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			this.closeSession();
		}

	}

	// pour delete tous les mesure texts par une mesure
	public void deleteMesureTexts(String flag_mesure) {
		if (this.isExistFlag_mesure(flag_mesure)) {
			List<MesureText> listMesureText = getListMesureTextByFlag_mesure(flag_mesure);
			this.deleAll(listMesureText);
		}
	}

	public void deleteAllMesureTexts(){
		List<MesureText> list = this.getAllListMesureText();
		if(!list.isEmpty()){
			this.deleAll(list);
		}
	}

	public static void main(String[] args) {

		MesureTextDAOHibernate e = new MesureTextDAOHibernate();

		// String N_serie = "1.1.150400122";
		//String flag_mesure = "10";
		// e.deleteMesureTexts(N_serie, flag_mesure);
		//e.deleteMesureTexts(flag_mesure);
		e.deleteAllMesureTexts();
		/*
		 * long mesureText_id = 5; MesureText mesureText=
		 * e.getMesureText(mesureText_id); boolean isexist =
		 * e.isExist_P2_mesure(mesureText); System.out.println(isexist);
		 */
	}

}
